//获取app应用
const app = getApp();

Page({
  data:{
    inputValue:"",
    hiddenLoading:true,
    historyMovies:[],
    movies:[]
  },
  onLoad:function(){
    var that = this;
    var history_movies = wx.getStorageSync("history_movies");

    if(history_movies != "")
    {
      var data = JSON.parse(history_movies);
      that.setData({
        historyMovies: data
      });
    }
  },
  bindKeyInput:function(e)
  {
    var that = this;
    that.setData({
      inputValue: e.detail.value,
    });
  },
  searchMovies:function()
  {
    //开始查询数据部分
    var that = this;

    var movie_name = that.data.inputValue;

    var history = that.data.historyMovies.concat(movie_name);

    that.setData({
      hiddenLoading: !that.data.hiddenLoading,
      historyMovies: history
    });

    //将搜索的历史记录放到缓存当中去
    wx.setStorageSync("history_movies", JSON.stringify(history));

    //获取热映电影
    wx.request({
      url: `https://douban.uieee.com/v2/movie/search?q=${movie_name}`,
      data:{},
      header:{"Content-Type":"json"},
      success:function(res)
      {
        console.log(res);
        that.setData({
          movies: res.data.subjects,
          hiddenLoading: !that.data.hiddenLoading
        });
      }
    })

  }
});